﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Coding4Fun.Phone.Controls;

namespace Store.WP7.Framework
{
    public enum ToastType
    {
        Neutral,
        OK,
        Error
    }

    public class ToastService
    {
        #region Singleton

        /// <summary>
        /// Configuration manager unique instance
        /// </summary>
        private static volatile ToastService instance;

        /// <summary>
        /// Sync object
        /// </summary>
        private static object syncRoot = new Object();

        /// <summary>
        /// Configuration manager instance
        /// </summary>
        public static ToastService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new ToastService();
                    }
                }

                return instance;
            }
        }

        private ToastService()
        {

        }

        #endregion

        ToastPrompt _lastToast;

        public void ShowNotification(ToastType type, string message, string title = null)
        {
            if (_lastToast != null)
                _lastToast.Hide();

            _lastToast = new ToastPrompt();

            _lastToast.TextOrientation = Orientation.Vertical;

            _lastToast.Title = title;
            _lastToast.Message = message;
            _lastToast.Overlay = null;

            _lastToast.Style = Application.Current.Resources["ToastPromptStyle"] as Style;

            switch (type)
            {
                case ToastType.Error:
                    _lastToast.Background = Application.Current.Resources["ErrorBrush"] as SolidColorBrush;
                    break;
                case ToastType.OK:
                    _lastToast.Background = Application.Current.Resources["AccentBrushDark"] as SolidColorBrush;
                    break;
                default:
                    _lastToast.Background = new SolidColorBrush(Colors.DarkGray);
                    break;
            }

            Deployment.Current.Dispatcher.BeginInvoke(() => _lastToast.Show());
        }

        public void ShowNotification(ToastType type, Action onTapAction, string message, string title = null)
        {
            if (_lastToast != null)
                _lastToast.Hide();

            _lastToast = new ToastPrompt();

            _lastToast.TextOrientation = Orientation.Vertical;

            _lastToast.Title = title;
            _lastToast.Message = message;
            _lastToast.Overlay = null;

            _lastToast.Style = Application.Current.Resources["ToastPromptStyle"] as Style;

            switch (type)
            {
                case ToastType.Error:
                    _lastToast.Background = Application.Current.Resources["ErrorBrush"] as SolidColorBrush;
                    break;
                case ToastType.OK:
                    _lastToast.Background = Application.Current.Resources["AccentBrushDark"] as SolidColorBrush;
                    break;
                default:
                    _lastToast.Background = new SolidColorBrush(Colors.DarkGray);
                    break;
            }

            EventHandler<GestureEventArgs> onTap = null;
            onTap = (s, e) =>
            {
                onTapAction();
                _lastToast.Tap -= onTap;
            };

            _lastToast.Tap += onTap;

            Deployment.Current.Dispatcher.BeginInvoke(() => _lastToast.Show());
        }
    }
}
